<?php
/*
 * Created on Jan 4, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class CommentsController extends AppController {
 	var $name = 'Comments';
 	
 	/*
 	 * Current Bug w/Comments::
 	 * 	When BlogID = CommentID, comments update on top of each other
 	 * 	not displaying correctly
	 * 
	 *  Fix was to add $this->Comment->create() 	even though Cake is supposed to do so automatically... 
	 *  
 	 */
 	function add($blogId = null) {
 		$this->autoRender = false;
 		$user = $this->Session->read('user');
 		$this->set(compact('user'));	
 		
 		if(!empty($this->data)) {
 			$this->data['Comment']['blog_id'] = $blogId;
 			$this->data['Comment']['user_id'] = $user['User']['id'];
 			$this->log($this->data, 'debug');
 			$this->Comment->create();
 			if ($this->Comment->save($this->data)) {
 				$this->Session->setFlash('Comment Added!');
 				$this->redirect(array('controller' => 'blogs', 'action' => 'viewBlog/' .$blogId));
 			}
 		}
 	}
	
	function getCommentsAJAX($blogId = null, $offset = null) {
		$this->autoRender = false;
		$comments = $this->Comment->find('all', 
			array('conditions' => array('Comment.blog_id' => $blogId), 
			'order' => array('Comment.modified' => 'DESC'),
			'recursive' => '-1',
			'offset' => $offset,
			'limit' => '2'
			));
		$count = $this->Comment->find('count', 
			array('conditions' => array('Comment.blog_id' => $blogId),
			'recursive' => '-1'));
		echo json_encode(array('comments' => $comments, 'count' => $count));
	}
	
 }
?>
